/*
spiCAN.h - Library for talking to MCP2551 CAN controller over SPI
 
 */
#ifndef spiCAN_h
#define spiCAN_h

#include "WProgram.h"
//#include "can_constants.h"
typedef struct{
  int sid;
  byte dlc;
  byte data[8];
} 
Datapack;

class spiCAN
{
public:
  boolean transmitstate;
  boolean transmitpins;

  int csPin;
  int rts0Pin;
  int rts1Pin;
  int rts2Pin;
  spiCAN(int CSPin,int RTS0Pin,int RTS1Pin,int RTS2Pin);
  void initController();
  void beginTrans(byte instr, byte data);
  void transfer(byte data);
  byte readbyte();
  void endTrans();
  void writeRegister(byte address,byte data[], byte numbytes);
  void writeRegister(byte address,byte data);
  byte readRegister(byte address);
  byte readRegisterCont(byte address);
  void bitModRegister(byte address,byte mask,byte data);
  Datapack readRXBuf(boolean bufnum);
  byte freeTXBuffer();
  void sendTXBuffer(byte adr);
  void sendCANMessage(int sid,byte dlc,byte data[]);
  void sendCANMessage(Datapack a);
private:
  void mode(byte config);
  byte spitransfer(byte value);
  void togglePinLow(int Pin);
    void togglePinHigh(int Pin);
};

#endif




